package warehouse;



/**
 * Order class creates order objects
 * checks for the validity of input in the constructor
 * @author Apeiranthitis, Grassos, Moschos
 *
 */
public class Order implements Comparable <Order> 
{
	//instance variables
	private String orderID;
	private String customerID;
	private String itemID;
	private int orderQuantity;

	//some static values to help us validate data
	private static final int MINIMUM_ITEM_ID = 100;
	private static final int MAXIMUM_ITEM_ID = 999;
	private static final int MINIMUM_ORDER_ID = 1;
	private static final int MAXIMUM_ORDER_ID = 1000000;
	private static final int MINIMUM_ORDER_QUANTITY = 1;


	/** Initializes Order class constructor, splitting the provided String by "," character,
	 *  calls init() method to check the values
	 * @param orderline
	 * @throws IllegalStateException
	 */
	public Order(String orderline) throws  IllegalStateException
	{   	

		String parts [] = orderline.split(",");
		this.orderID =parts[0].trim();
		this.customerID= parts[1].trim();
		this.itemID=parts[2].trim();
		String quantity=parts[3].trim();;
		int quant = Integer.parseInt(quantity);
		orderQuantity=quant;

		init();
	}

	/**
	 * Initializes the constructor parameters, checking for inappropriate input and if finds it so, 
	 * changes the Item ID to "Wrong" making us able to locate the error later. 
	 * Additionally, it catches a potential NmberFormatException in case the item id is not an
	 * arithmetic value
	 */
	public void init() {
		try{
			//check if Item ID is between 100 and 999
			if ( orderID.length() ==0 || Integer.parseInt(orderID) < MINIMUM_ORDER_ID || 
					Integer.parseInt(orderID) > MAXIMUM_ORDER_ID ){
				System.out.println("Order id: "+orderID+"(Out of range Order ID.) ");
				orderID="Wrong";	

			}
			
			//check if Order ID is empty field
			if ( orderID.isEmpty() || orderID.equals("") ) {

				System.out.println("Order id: "+orderID+" (Order ID field is empty.)");
				orderID="Wrong";
			}
			
			//check if Item ID is between [0-999]
			if ( itemID.length() < 0 || Integer.parseInt(itemID) < MINIMUM_ITEM_ID || 
					Integer.parseInt(itemID) > MAXIMUM_ITEM_ID ){

				System.out.println("Order id: "+orderID+" (Out of range Item ID.) ");
				orderID="Wrong";
			}
			
			//check if ItemID is empty field
			if ( itemID.isEmpty() || itemID.equals("") ) {

				System.out.println("Order id: "+orderID+" (Item ID field is empty.)");
				orderID="Wrong";
			}

			//check if customer ID is a correct id
			if ( customerID.length() != 4 || !customerID.substring(0,1).matches("[A-C]") &&  
					!customerID.substring(1, 3).matches("[100-999]")) {

				System.out.println("Order id: "+orderID+" (Error in Customer ID input: "+customerID+".)");
				orderID="Wrong";
			}
			
			//check if order quantity is negative
			if(orderQuantity<MINIMUM_ORDER_QUANTITY){
				System.out.println("Order id: "+orderID+" (Error in Quantity input: "+orderQuantity+".)");
				orderID="Wrong";
			}
		}
		catch(NumberFormatException nfe){
			System.out.println("Wrong input in Order/Item Id");
			orderID="Wrong";
		}
	}

	//some get methods
	public String getOrderID() {
		return orderID;
	}

	public String getCustomerID() {
		return customerID;
	}

	public String getItemID() {
		return itemID;
	}

	public int getOrderQuantity() {
		return orderQuantity;
	}


	/**
	 * Test for content equality between two objects.
	 * @param other The object to compare to this one.
	 * @return true if the argument object has same id
	 */
	public boolean equals(Object other)
	{
		if(other instanceof Order) {
			Order otherItem = (Order) other;
			return orderID.equals(otherItem.getOrderID());
		}
		else {
			return false;
		}
	}

	/**
	 * Compare this object against another, for the purpose
	 * of sorting. The fields are compared by id.
	 * @param otherDetails The details to be compared against.
	 * @return a negative integer if this id comes before the parameter's id,
	 *         zero if they are equal and a positive integer if this
	 *         comes after the other.
	 */

	public int compareTo(Order otherDetails)
	{
		return orderID.compareTo(otherDetails.getOrderID());
	}    

	/**
	 * Overrides toStig method
	 * @return A string containing all details.
	 */
	public String toString()
	{
		return "order id: "+orderID+" customer id: "+customerID+" item id: "+ itemID +
				" order quantity: "+orderQuantity ;
	}
	


}